package com.example.controller;

import com.example.common.CustomException;
import com.example.common.Result;
import com.example.common.ResultCode;
import com.example.common.ServiceCode;
import com.example.dto.OrderPageDTO;
import com.example.entity.GoodsInfo;
import com.example.entity.OrderInfo;
import com.example.entity.Total;
import com.example.service.OrderInfoService;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/orderInfo")
@Api(tags = "订单模块")
public class OrderInfoController {
    @Resource
    private OrderInfoService orderInfoService;


    @ApiOperation("查询营业额")
    @GetMapping("/getTotal")
    public Result<Total> getTotal() {
        Double totalYear = this.orderInfoService.getTotalYear();
        Double total2023 = 1500.0;
        Double totalMonth = this.orderInfoService.getTotalMonth();
        Double total5 = 1000.0;
        Total total = new Total();
        total.setTotalYear(totalYear);
        total.setTotalMonth(totalMonth);

        total.setTotalYearPercentage((int) ((double) totalYear/total2023 * 100 -100));
        total.setTotalMonthPercentage((int) ((double) totalMonth/total5 * 100 -100));
        return Result.success(total);
    }


    /**
     * 查询所有的订单信息（包括订单中的菜品列表)
     * 注意此处菜单列表中的数量count是订单中菜品的数量并不是菜品本身的库存
     * @return
     */
    @ApiOperation("查询所有的订单信息（包括订单中的菜品列表)")
    @GetMapping("/getAllOrderInfoWithGoods")
    public Result<List<OrderInfo>> getAllOrderInfoWithGoods() {
        List<OrderInfo> orderInfos = this.orderInfoService.getAllOrderInfoWithGoods();
        return Result.success(orderInfos);

    }

    /**
     * 根据订单主键id查询
     */
    @ApiOperation("根据订单主键id查询")
    @GetMapping("/getById/{id}")
    public Result<OrderInfo> findById(@PathVariable Long id) {
        return Result.success(orderInfoService.findById(id));
    }


    /**
     * 查询某个用户的所有订单信息
     */
    @ApiOperation("查询某个用户的所有订单信息")
    @GetMapping("/getAllByUser")
    public Result<List<OrderInfo>> findAll(@RequestParam Long userId, @RequestParam Integer level) {
        return Result.success(orderInfoService.findAll(userId, level));
    }

//    /**
//     * 查询所有信息（分页）
//     */
//    @GetMapping("/page")
//    public Result<PageInfo<OrderInfo>> findPage(@RequestParam(required = false, defaultValue = "1") Integer pageNum,
//                                                @RequestParam(required = false, defaultValue = "10") Integer pageSize,
//                                                HttpServletRequest request) {
//        return Result.success(orderInfoService.findEndPages(pageNum, pageSize, request));
//    }

    /**
     * 查询订单信息（分页）
     * 包括模糊查询
     */
    @ApiOperation("查询订单信息（分页）")
    @PostMapping("/page")
    public Result<PageInfo<OrderInfo>> findPage(@RequestBody OrderPageDTO orderPageDTO) {
        return Result.success(orderInfoService.findEndPages(orderPageDTO));
    }
//
//    /**
//     * 查询所有信息（分页）
//     */
//    @GetMapping("/page/front")
//    public Result<PageInfo<OrderInfo>> findFrontPage(@RequestParam(required = false) Long userId,
//                                                     @RequestParam(required = false) Integer level,
//                                                     @RequestParam(required = false, defaultValue = "1") Integer pageNum,
//                                                     @RequestParam(required = false, defaultValue = "10") Integer pageSize) {
//        return Result.success(orderInfoService.findFrontPages(userId, level, pageNum, pageSize));
//    }

    /**
     * 下单
     *
     * @return
     */
    @ApiOperation("下单")
    @PostMapping
    public Result<OrderInfo> add(@RequestBody OrderInfo orderInfo) {
        Long userId = orderInfo.getUserId();
        List<GoodsInfo> goodsList = orderInfo.getGoodsList();
        if (userId == null || goodsList == null || goodsList.size() == 0) {
            throw new CustomException(ResultCode.PARAM_ERROR);
        }
        return Result.success(orderInfoService.add1(orderInfo));
    }

    /**
     * 根据订单id修改订单信息
     * @param orderInfo
     * @return
     */
    @PutMapping("/update")
    public Result<OrderInfo> update(@RequestBody OrderInfo orderInfo) {
        OrderInfo updatedOrderInfo = orderInfoService.updateOrder(orderInfo);
        if (updatedOrderInfo != null) {
            return Result.success(updatedOrderInfo);
        } else {
            return Result.error(String.valueOf(ServiceCode.ERR_CONFLICT), "更新订单失败");
        }
    }

    /**
     * 选择堂食或外卖
     */
    @PostMapping("/placeOrder")
    public Result placeOrder(@RequestParam("id") Long id,@RequestParam("orderType") Integer orderType) {
        this.orderInfoService.placeOrder(id, orderType);
        return  Result.success();
    }

    /**
     *  修改订单状态
     */
    @PutMapping("/changeStatus")
    public Result status(@RequestParam("id") Long id, @RequestParam("status") String status) {
        orderInfoService.changeStatus(id, status);
        return Result.success();
    }

    /**
     * 删除某个订单
     * @param id
     * @return
     */
    @DeleteMapping()
    public Result delete(@RequestParam Long id) {
        this.orderInfoService.deleteOrderById(id);
        return Result.success();
    }

    @ApiOperation("查找用户上一次下单的订单")
    @GetMapping("/getOnce/{userId}")
    public Result<OrderInfo> getOnce(@PathVariable Long userId) {
        OrderInfo orderInfo = this.orderInfoService.getOnce(userId);
        return Result.success(orderInfo);
    }


//    /**
//     * 删除历史订单
//     */
//    @DeleteMapping("/{id}")
//    public Result delete(@PathVariable Long id) {
//        orderInfoService.delete(id);
//        return Result.success();
//    }

//    @DeleteMapping()
//    public Result delete(@RequestParam Long goodsId, @RequestParam Long id) {
//        orderInfoService.deleteGoods(goodsId, id);
//        return Result.success();
//    }

//    @GetMapping("/queryOrderInfoByStatus")
//    public ResponseData queryOrderInfoByStatus(){
//        return orderInfoService.queryOrderInfoByStatus();
//    }
//
//    @GetMapping("/updataState")
//    public ResponseData updataState(String i){
//        return orderInfoService.updataState(i);
//    }
//
//    @GetMapping("/queryOrderInfoByOrderId")
//    public ResponseData queryOrderInfoByOrderId(String orderId){
//        return orderInfoService.queryOrderInfoByOrderId(orderId);
//    }
}
